package com.example.dllo.keepdemo.train.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.example.dllo.keepdemo.R;
import com.example.dllo.keepdemo.greendao.TraniPlan;
import com.example.dllo.keepdemo.tools.DoubleCache;
import com.example.dllo.keepdemo.tools.VolleySingleton;
import com.example.dllo.keepdemo.train.bean.TrainFragmentBean;

import java.util.List;

/**
 * Created by dllo on 16/1/12.
 */
public class PlanListViewAdapter extends BaseAdapter {
    private Context context;
    private List<TrainFragmentBean.DataEntity.PlansEntity> plansEntityList;
    private List<TraniPlan> traniPlanList;
    //test = 1 走数据库没网
    private int test;

    public PlanListViewAdapter(Context context, List<TrainFragmentBean.DataEntity.PlansEntity> plansEntityList,int hasNet) {
        this.context = context;
        this.plansEntityList = plansEntityList;
        this.test = hasNet;
    }

    public PlanListViewAdapter(List<TraniPlan> traniPlanList, Context context,int noneNet) {
        this.context = context;
        this.traniPlanList = traniPlanList;
        this.test = noneNet;
    }

    @Override
    public int getCount() {
        if (test == 1) {
            return traniPlanList.size();
        } else {
            return plansEntityList.size();
        }
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 1;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_listview_fragment_train_plan, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        if (test == 1) {
            viewHolder.tvTitle.setText(traniPlanList.get(position).getTitle());
            viewHolder.tvDifficulty.setText("K" + traniPlanList.get(position).getDifficulty());
            switch (Integer.parseInt(traniPlanList.get(position).getDifficulty())) {
                case 1:
                    viewHolder.tvFoundations.setText("零基础");
                    break;
                case 2:
                    viewHolder.tvFoundations.setText("初学");
                    break;
                case 3:
                    viewHolder.tvFoundations.setText("进阶");
                    break;
                case 4:
                    viewHolder.tvFoundations.setText("强化");
                    break;
                case 5:
                    viewHolder.tvFoundations.setText("挑战");
                    break;
            }
            viewHolder.tvEquipments.setText(traniPlanList.get(position).getEquipments());
            viewHolder.tvTime.setText(traniPlanList.get(position).getTime() + "节");
            viewHolder.tvBody.setText(traniPlanList.get(position).getBody());
            viewHolder.tvJoin.setText(traniPlanList.get(position).getJoin() + "人已参加");
            viewHolder.ratingBar.setRating(Float.parseFloat(traniPlanList.get(position).getDifficulty()));

            ImageLoader.ImageListener listener = ImageLoader.getImageListener(viewHolder.imageView, R.mipmap.plan320_160, R.mipmap.no_photo);
            VolleySingleton.getImageLoader().get(traniPlanList.get(position).getUrl(), listener, 400, 150);

        } else {
            viewHolder.tvTitle.setText(plansEntityList.get(position).getName());
            viewHolder.tvDifficulty.setText("K" + plansEntityList.get(position).getDifficulty());
            switch (plansEntityList.get(position).getDifficulty()) {
                case 1:
                    viewHolder.tvFoundations.setText("零基础");
                    break;
                case 2:
                    viewHolder.tvFoundations.setText("初学");
                    break;
                case 3:
                    viewHolder.tvFoundations.setText("进阶");
                    break;
                case 4:
                    viewHolder.tvFoundations.setText("强化");
                    break;
                case 5:
                    viewHolder.tvFoundations.setText("挑战");
                    break;
            }
            viewHolder.tvEquipments.setText(plansEntityList.get(position).getEquipments().get(0).getName());
            viewHolder.tvTime.setText(plansEntityList.get(position).getDays() + "节");
            viewHolder.tvBody.setText(plansEntityList.get(position).getTrainingpoints().get(0).getName());
            viewHolder.tvJoin.setText(plansEntityList.get(position).getPioneer() + "人已参加");
            viewHolder.ratingBar.setRating((float) plansEntityList.get(position).getDifficulty());

            ImageLoader.ImageListener listener = ImageLoader.getImageListener(viewHolder.imageView, R.mipmap.plan320_160, R.mipmap.no_photo);
            VolleySingleton.getImageLoader().get(plansEntityList.get(position).getPicture(), listener, 400, 150);
        }


        return convertView;
    }

    class ViewHolder {
        private TextView tvTitle, tvDifficulty, tvFoundations, tvEquipments, tvTime, tvBody, tvJoin;
        private ImageView imageView;
        private RatingBar ratingBar;

        public ViewHolder(View view) {
            tvTitle = (TextView) view.findViewById(R.id.tv_item_train_plan_title);
            tvDifficulty = (TextView) view.findViewById(R.id.tv_item_train_plan_difficulty);
            tvFoundations = (TextView) view.findViewById(R.id.tv_item_train_plan_foundations);
            tvEquipments = (TextView) view.findViewById(R.id.tv_item_train_plan_equipments);
            tvTime = (TextView) view.findViewById(R.id.tv_item_train_plan_whole_time);
            tvBody = (TextView) view.findViewById(R.id.tv_item_train_plan_whole_body);
            tvJoin = (TextView) view.findViewById(R.id.tv_item_train_plan_join_person_number);
            ratingBar = (RatingBar) view.findViewById(R.id.rating_item_train_plan_star);
            imageView = (ImageView) view.findViewById(R.id.img_item_train_plan);
        }
    }
}
